package com.example.qiyuesuotwo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.qiyuesuotwo.ResponseResult;
import com.qiyuesuo.sdk.SDKClient;
import com.qiyuesuo.sdk.api.SealService;
import com.qiyuesuo.sdk.impl.SealServiceImpl;
import com.qiyuesuo.sdk.seal.Seal;
import com.qiyuesuo.sdk.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seal")
public class sealCreate {
    private static final Logger logger = LoggerFactory.getLogger(sealCreate.class);
    @Value("${url}")
    private String qiyuesuoUrl;

    @Value("${accessKey}")
    private String qiyuesuoAccessKey;

    @Value("${accessSecret}")
    private String qiyuesuoAccessSecret;

    @ResponseBody
    @RequestMapping("/create")
    public void createSeal(){
        ResponseResult result = new ResponseResult();
        Map<String, Object> map = new HashMap<String, Object>();

    }
    @ResponseBody
    @RequestMapping("/list")
    public void listSeal(){
        ConfigurableApplicationContext context = SpringApplication.run(sealCreate.class);
        context.registerShutdownHook();
        context.start();
        SealService sealService = context.getBean(SealService.class);
        ResponseResult result = new ResponseResult();
        Map<String, Object> map = new HashMap<String, Object>();
        //获取所有可用的平台印章
        List<Seal> sealList = sealService.sealList();

        logger.info("获取所有可用的平台印章完成,结果:{}", JsonUtils.toJson(sealList));
    }

    @Bean
    public SDKClient sdkClient(){
        String url = qiyuesuoUrl; //测试环境
        String accessKey = qiyuesuoAccessKey;
        String accessSecret = qiyuesuoAccessSecret;
        return new SDKClient(url,accessKey,accessSecret);
    }

    @Bean
    public SealService sealService(SDKClient sdkClient) {
        return new SealServiceImpl(sdkClient);
    }
}
